#include "PointLightComponent.h"
#include "SceneSystem.h"

IEngineComponentRegistry pointLightComponentRegistery("PointLightComponent", PointLightComponent::Deserialize);

PointLightComponent::PointLightComponent()
{
	this->position = D3DXVECTOR3(0,0,0);
	this->color = D3DXVECTOR3(1,1,1);
	this->radius = (float)D3DX_PI;
	this->intensity = 1;
}

PointLightComponent::PointLightComponent(D3DXVECTOR3 position, D3DXVECTOR3 color, float radius, float intensity)
{
	this->position = position;
	this->color = color;
	this->radius = radius;
	this->intensity = intensity;
}

PointLightComponent::~PointLightComponent(void)
{
}

D3DXVECTOR3 PointLightComponent::getPosition()
{
	return position;
}

void PointLightComponent::setPosition(D3DXVECTOR3 position)
{
	this->position = position;
}

D3DXVECTOR3 PointLightComponent::getColor()
{
	return color;
}

void PointLightComponent::setColor(D3DXVECTOR3 color)
{
	this->color = color;
}

float PointLightComponent::getRadius()
{
	return radius;
}

void PointLightComponent::setRadius(float radius)
{
	this->radius = radius;
}

float PointLightComponent::getIntensity()
{
	return intensity;
}

void PointLightComponent::setIntensity(float intensity)
{
	this->intensity = intensity;
}

D3DXMATRIX PointLightComponent::getWorld()
{
	D3DXMATRIX mScale;
	D3DXMATRIX mPosition;
	D3DXMatrixScaling(&mScale, radius, radius, radius);
	D3DXMatrixTranslation(&mPosition, position.x, position.y, position.z);
	return mScale * mPosition;
}

void PointLightComponent::Serialize(EnginePointer<ISerializerWriteContext>& context, char* name)
{
	context->StartItem(name, "PointLightComponent", "1.0");
	context->SerializeProperty("componentId", this->componentId);
	context->SerializeProperty("radius", radius);
	context->SerializeProperty("intensity", intensity);
	context->SerializeProperty("positionX", position.x);
	context->SerializeProperty("positionY", position.y);
	context->SerializeProperty("positionZ", position.z);
	context->SerializeProperty("colorR", color.x);
	context->SerializeProperty("colorG", color.y);
	context->SerializeProperty("colorB", color.z);
	context->EndItem(name);
}

IEngineComponent* PointLightComponent::Deserialize(EnginePointer<ISerializerReadContext>& context)
{
	PointLightComponent* pointLight = new PointLightComponent();
	int index = 0;
	char* childName = context->ChildName(index);

	while (childName != NULL)
	{
		if (strcmp(childName, "componentId") == 0)
		{
			pointLight->componentId = context->ChildValueAsULongLong(index);
		}
		else if (strcmp(childName, "radius") == 0)
		{
			float radius = context->ChildValueAsFloat(index);
			pointLight->setRadius(radius);
		}
		else if (strcmp(childName, "intensity") == 0)
		{
			float intensity = context->ChildValueAsFloat(index);
			pointLight->setIntensity(intensity);
		}
		else if(strcmp(childName, "positionX") == 0)
		{
			D3DXVECTOR3 position = pointLight->getPosition();
			position.x = context->ChildValueAsFloat(index);
			pointLight->setPosition(position);
		}
		else if(strcmp(childName, "positionY") == 0)
		{
			D3DXVECTOR3 position = pointLight->getPosition();
			position.y = context->ChildValueAsFloat(index);
			pointLight->setPosition(position);
		}
		else if(strcmp(childName, "positionZ") == 0)
		{
			D3DXVECTOR3 position = pointLight->getPosition();
			position.z = context->ChildValueAsFloat(index);
			pointLight->setPosition(position);
		}
		else if(strcmp(childName, "colorR") == 0)
		{
			D3DXVECTOR3 color = pointLight->getColor();
			color.x = context->ChildValueAsFloat(index);
			pointLight->setColor(color);
		}
		else if(strcmp(childName, "colorG") == 0)
		{
			D3DXVECTOR3 color = pointLight->getColor();
			color.y = context->ChildValueAsFloat(index);
			pointLight->setColor(color);
		}
		else if(strcmp(childName, "colorB") == 0)
		{
			D3DXVECTOR3 color = pointLight->getColor();
			color.z = context->ChildValueAsFloat(index);
			pointLight->setColor(color);
		}
		childName = context->ChildName(++index);
	}
	
	return pointLight;
}

GUID PointLightComponent::getSystemIdentifier()
{
	return SCENE_SYSTEM_GUID;
}

GUID PointLightComponent::getComponentIdentifier()
{
	return POINT_LIGHT_COMPONENT_GUID;
}